package com.whu.waterworks.action;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.PropertyUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.whu.waterworks.dto.WaterPlantDataDto;
import com.whu.waterworks.dto.WaterPlantMainDataDto;
import com.whu.waterworks.model.TWaterplantdata;
import com.whu.waterworks.model.WaterPlant;
import com.whu.waterworks.service.WaterPlantDataService;
import com.whu.waterworks.service.WaterPlantService;
/**
 * 水厂实时数据相关action
 * @author wangying
 *
 */
public class RealTimeWaterPlantDataAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String waterPlantCode;
	private WaterPlantService waterPlantService;
	private WaterPlantDataService waterPlantDataService;
	
	
	private List<WaterPlantMainDataDto> waterPlantMainDataDtos;
	
	private List<WaterPlantMainDataDto> waterPlantMainDataDto;
	private List<WaterPlantDataDto> waterPlantDataDtos;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		waterPlantMainDataDtos  = new ArrayList<>();
		List<WaterPlant> waterPlants = waterPlantService.getAllWaterPlants();
		if(waterPlants!=null && waterPlants.size()>0)//所有水厂遍历
		{
			for(WaterPlant waterPlant:waterPlants)
			{
				TWaterplantdata waterplantdata = waterPlantDataService.getLastWaterPlantData(waterPlant.getWaterPlantCode());
				if(waterplantdata!=null )
				{					
					WaterPlantMainDataDto waterPlantMainDataDto = new WaterPlantMainDataDto();
					waterPlantMainDataDto.setWaterPlantName(waterPlant.getWaterPlantName());
					waterPlantMainDataDto.setTime(waterplantdata.getId().getTime());
					waterPlantMainDataDto.setChlorine(waterplantdata.getChlorineOut());
					waterPlantMainDataDto.setSupplyFlow(waterplantdata.getTotalSupplyFlow());
					waterPlantMainDataDto.setTurbidity(waterplantdata.getTurbidity());				
					waterPlantMainDataDtos.add(waterPlantMainDataDto);					
				}
			}
		}
		return super.execute();
	}
	
	public String getRealWaterPlantDataById()
	{
		waterPlantDataDtos  = new ArrayList<>();
		TWaterplantdata waterplantdata = waterPlantDataService.getLastWaterPlantData(waterPlantCode);
		WaterPlantDataDto waterPlantMainDataDtotemp = new WaterPlantDataDto();
		try {
			PropertyUtils.copyProperties(waterPlantMainDataDtotemp, waterplantdata);
			waterPlantMainDataDtotemp.setWaterPlantName(waterPlantService.getWaterPlantById(waterPlantCode).getWaterPlantName());
			waterPlantMainDataDtotemp.setTime(waterplantdata.getId().getTime());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		waterPlantDataDtos.add(waterPlantMainDataDtotemp);
		
		return SUCCESS;
	}
	
	public WaterPlantService getWaterPlantService() {
		return waterPlantService;
	}

	public void setWaterPlantService(WaterPlantService waterPlantService) {
		this.waterPlantService = waterPlantService;
	}

	public WaterPlantDataService getWaterPlantDataService() {
		return waterPlantDataService;
	}

	public void setWaterPlantDataService(WaterPlantDataService waterPlantDataService) {
		this.waterPlantDataService = waterPlantDataService;
	}

	public List<WaterPlantMainDataDto> getWaterPlantMainDataDtos() {
		return waterPlantMainDataDtos;
	}

	public void setWaterPlantMainDataDtos(List<WaterPlantMainDataDto> waterPlantMainDataDtos) {
		this.waterPlantMainDataDtos = waterPlantMainDataDtos;
	}

	public String getWaterPlantCode() {
		return waterPlantCode;
	}

	public void setWaterPlantCode(String waterPlantCode) {
		this.waterPlantCode = waterPlantCode;
	}

	public List<WaterPlantMainDataDto> getWaterPlantMainDataDto() {
		return waterPlantMainDataDto;
	}

	public void setWaterPlantMainDataDto(
			List<WaterPlantMainDataDto> waterPlantMainDataDto) {
		this.waterPlantMainDataDto = waterPlantMainDataDto;
	}

	public List<WaterPlantDataDto> getWaterPlantDataDtos() {
		return waterPlantDataDtos;
	}

	public void setWaterPlantDataDtos(List<WaterPlantDataDto> waterPlantDataDtos) {
		this.waterPlantDataDtos = waterPlantDataDtos;
	}

	

	

}
